<template>
	<div>
	  <van-popup
		  v-model="showWin"
		  position="right"
		  :style="{ height: '100%',width:'100%'}"
	  >
		<van-nav-bar 
		  class="fix_header"
		  title="编辑收货地址"
		  left-arrow
		>		
		  <van-icon name="arrow-left" slot="left" color="#000" size="20" @click="showWin=false"/>
	    </van-nav-bar>	
	    <div class="fix_next"></div>
	    
	    <div class="input">
			<van-cell-group>
			  <van-field v-model="params.addressRealName" placeholder="请输入收货人姓名" label="收货人"/>			  
			</van-cell-group>	
			<van-cell-group>
			  <van-field v-model="params.addressMobile" placeholder="请输入收货人联系电话" label="手机号"/>
			</van-cell-group>
			
			<van-cell-group>
			  <van-field v-model="params.addressAreaInfo" placeholder="请选择地区"  right-icon="arrow" 
			  	label="地区选择" readonly  @click="showArea"/>
			</van-cell-group>			
			
	    	
			<van-cell-group>
				<van-field
				  v-model="params.address"
				  rows="5"
				  autosize
				  label="详细地址"
				  type="textarea"
				  placeholder="请输入详细地址"
				  
				/>
			</van-cell-group>
			
			
			
			<div class="defaultWrap">
				<div class="left">
					<div class="word1" style="font-size: 14px;">设为默认地址</div>
					<div class="word2" style="margin-top: 10px;color: #999999;">每次下单会默认使用该地址，方便您轻松购物</div>
				</div>
				<div class="right">
                    <van-switch v-model="params.addressIsDefault" size="20px" active-color="#FFD630" inactive-color="#999"/>				
				</div>
			</div>
			

			
	    </div>



        <floor46 ref="floor46" @query="selectArea" :areaValue="params.addressAreaId3"></floor46>



	    <div class="fixB" >
	    	<van-button type="primary"  class="bottom" :loading="loading" @click="save">保存地址</van-button>
	    </div>
	  </van-popup>	
	</div>	
</template>
<script>
import areaList from '@/api/area.js'
import {getFun} from '@/api/publicFun.js'	
import Floor46 from '_c/market/floor/floor46.vue'	
let publicFun=getFun()	
export default {
	components: {
	     Floor46
	},	
	data() {
       return {
       	 showWin:false,
       	 loading:false,
       	 show:false,// 显示地区选择
       	 areaList:areaList,   // 地区选择的数据文件
         url3:this.$api+'/marketorderservice/api/v1/market/address/edit', // 编辑地址接口
         params:{
         	addressId:'',
			address:'',           // 详细地址 ,
			addressAreaId1:'',    // 一级地区Id ,
			addressAreaId2:'',    // 二级地区Id ,
			addressAreaId3:'',    // 三级地区Id ,
			addressIsDefault:false,  // 是否是默认收货地址(0-否，1-是) ,
			addressMobile:'',     // 手机 ,
			addressRealName:'',   // 收货人姓名 ,
			memberId:'',          // 会员Id     
			addressAreaInfo:''    // 拼接地区 
         }
       }
	},
	methods: {
		showPopup(item){ 
			this.showWin=true
			this.params.address=item.address
			this.params.addressId=item.addressId
			this.params.addressAreaId1=item.addressAreaId1
			this.params.addressAreaId2=item.addressAreaId2
			this.params.addressAreaId3=item.addressAreaId3.toString()
			this.params.addressMobile=item.addressMobile
			this.params.addressRealName=item.addressRealName
			this.params.memberId=item.memberId
			this.params.addressAreaInfo=item.addressAreaInfo
			this.params.addressIsDefault=item.addressIsDefault==1?true:false
		},
		showArea(){
			this.$refs.floor46.show()
		},
		// 确认省市区
        selectArea(arr){
        	this.params.addressAreaId1=arr[0].code
        	this.params.addressAreaId2=arr[1].code
        	this.params.addressAreaId3=arr[2].code
        	let title=arr[0].name+' '+ arr[1].name +' ' + arr[2].name 
        	this.params.addressAreaInfo=title
        },
        save(){        	
        	if(!this.regTel(this.params.addressMobile)){
        		this.$toast('请输入正确的电话号码')
        		return false
        	}        	
        	if(this.params.addressAreaId1==''||this.params.addressAreaId2==''||this.params.addressAreaId3==''){
        		this.$toast('请选择省市区')
        		return false
        	}
        	if(!this.params.address){
        		this.$toast('详细地址不能为空')
        		return false        		
        	}
        	if(!this.params.addressRealName){
        		this.$toast('收货人姓名不能为空')
        		return false        		
        	}        	
            this.params.addressIsDefault=this.params.addressIsDefault?1:0  
            this.loading=true
	      	let that=this	
	      	console.log(this.params)
		    this.$Axios2.Post(this.url3,this.params)
		    .then(function(res) {
		       that.$toast('操作成功')
               that.showWin=false
               that.loading=false
               that.$emit('reload')
		    })	            
        }
	},
	mixins:[publicFun],//混入		
	mounted() {
		this.params.memberId=this.$router.currentRoute.query.userId
	}
}	
</script>

<style scoped lang="scss">
.bottom{background: $bc1;border: none;width: 100%;border-radius: 0;color: #333;font-size: 16px;font-weight: 500;}
.fixB{padding: 20px 37.5px;position: fixed;bottom: 0;left: 0;width: 100%;box-sizing: border-box;background: #fff;z-index: 1000;}

.input{padding-bottom: 100px;}
.defaultWrap{display: flex;padding: 10px 15px;align-items: center;}
.defaultWrap .left{width: 80%;}
.defaultWrap .right{flex: 1;display: flex;justify-content: flex-end;}
</style>